<template>
  <div class="about">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>人事管理</el-breadcrumb-item>
      <el-breadcrumb-item>绩效管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索与添加按钮 -->
      <el-form :inline="true" ref="info" :model="info">
        <el-form-item prop="jobNumber">
          <el-input
            v-model="info.jobNumber"
            style="width: 160px"
            placeholder="请输入工号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="startTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.startTime"
            start-placeholder="开始日期"
            placeholder="请选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.endTime"
            end-placeholder="结束日期"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            size="medium"
            @click="resetForm('info')"
            round
            >重置</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" @click="getUserList" round
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="4"
          ><el-button type="primary" @click="addDialogVisible = true"
            >添加绩效信息</el-button
          ></el-col
        >
      </el-row>
      <!-- 绩效列表 -->
      <el-table border stripe :data="userList">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="日期" prop="date"> </el-table-column>
        <el-table-column label="工号" prop="jobNumber"> </el-table-column>
        <el-table-column label="绩效金额" prop="meritMoney"> </el-table-column>
        <el-table-column label="绩效来源" prop="meritFrom"> </el-table-column>
        <el-table-column label="备注" prop="remark"> </el-table-column>
        <el-table-column label="操作" width="145px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showEditDialog(scope.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.currentPage"
        :page-sizes="[2, 6, 10, 100]"
        :page-size="info.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加绩效管理 -->
      <el-dialog
        title="添加绩效信息"
        :visible.sync="addDialogVisible"
        width="29%"
        @close="addDialogClosed"
      >
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="工号" prop="jobNumber">
            <el-input v-model="addForm.jobNumber"></el-input>
          </el-form-item>
          <el-form-item label="工资月份" prop="date">
            <el-date-picker v-model="addForm.date"></el-date-picker>
          </el-form-item>
          <el-form-item label="绩效金额" prop="meritMoney">
            <el-input v-model="addForm.meritMoney"></el-input>
          </el-form-item>
          <el-form-item label="绩效来源" prop="meritFrom">
            <el-input v-model="addForm.meritFrom"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="addForm.remark"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBum">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 绩效信息编辑 -->
      <el-dialog
        title="更新编辑绩效信息"
        :visible.sync="editDialogVisible"
        width="30%"
        @close="editDialogClosed"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="90px"
        >
          <el-form-item label="工号" prop="jobNumber">
            <el-input v-model="editForm.jobNumber"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-input v-model="editForm.date"></el-input>
          </el-form-item>
          <el-form-item label="绩效金额" prop="meritMoney">
            <el-input v-model="editForm.meritMoney"></el-input>
          </el-form-item>
          <el-form-item label="绩效来源" prop="meritFrom">
            <el-input v-model="editForm.meritFrom"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="editForm.remark"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: {
        currentPage: 1,
        size: 4,
        jobNumber: "",
        startTime: "",
        endTime: "",
      },
      userList: [],
      total: 0,
      // 添加绩效信息对话框隐藏
      addDialogVisible: false,
      //添加绩效信息的表单数据
      addForm: {
        jobNumber: "",
        date: "",
        meritMoney: "",
        meritFrom: "",
        remark: "",
        salaryId: "",
        from: "",
      },
      //添加表单的规则验证对象
      addFormRules: {
        jobNumber: [
          { required: true, message: "请输入工号", trigger: "blur" },
          {
            min: 1,
            max: 5,
            message: "工号名称为1~5个字符之间",
            trigger: "blur",
          },
        ],

        date: [{ required: true, message: "请输入月份", trigger: "blur" }],
        meritFrom: [
          { required: true, message: "请输入绩效来源", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "绩效来源为1~50个字符之间",
            trigger: "blur",
          },
        ],
        meritMoney: [
          { required: true, message: "请输入绩效金额", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "绩效金额为1~50个字符之间",
            trigger: "blur",
          },
        ],
        remark: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
        salaryId: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
      },
      //编辑绩效信息对话框隐藏
      editDialogVisible: false,
      //查询到的绩效信息对象
      editForm: {},
      //编辑绩效信息规则
      editFormRules: {
        jobNumber: [{ required: true, message: "请输入工号", trigger: "blur" }],

        date: [{ required: true, message: "请输入日期", trigger: "blur" }],
        meritMoney: [
          { required: true, message: "请输入绩效金额", trigger: "blur" },
        ],
        meritFrom: [
          { required: true, message: "请输入绩效来源", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "绩效来源为1~50个字符之间",
            trigger: "blur",
          },
        ],
        remark: [
          { required: true, message: "请输入备注", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取绩效信息
    async getUserList() {
      await this.$http
        .get("admin/selectMerit", {
          params: this.info,
        })
        .then((res) => {
          console.log(res);
          if (res.data.flag !== true) {
            return this.$message.error("获取绩效信息失败!");
          }
          this.userList = res.data.rows;
          this.total = res.data.total;
        });
    },
    //监听信息条数
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.info.size = newSize;
      this.getUserList();
    },
    //监听页数
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.info.currentPage = newPage;
      this.getUserList();
    },
    //监听添加绩效信息表单的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮添加绩效信息
    addBum() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起添加绩效信息的网络请求
        const { data: res } = await this.$http.post(
          "admin/insertMerit",
          this.addForm
        );
        if (res.flag !== true) {
          return this.$message.error("添加绩效信息失败!");
        }
        this.$message.success("添加绩效信息成功!");
        //隐藏添加绩效信息的对话框
        this.addDialogVisible = false;
        //重新获取绩效信息列表数据
        this.getUserList();
      });
    },
    //更新编辑绩效信息
    async showEditDialog(id) {
      console.log(id);
      const { data: res } = await this.$http.get("admin/selectMeritById", {
        params: { id },
      });
      if (res.flag !== true) {
        return this.$message.error("获取绩效信息失败!");
      }
      console.log(res);
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    // 监听更新绩效对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //更新绩效信息并提交
    editInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("admin/updateMerit", {
          id: this.editForm.id,
          jobNumber: this.editForm.jobNumber,
          date: this.editForm.date,
          meritMoney: this.editForm.meritMoney,
          meritFrom: this.editForm.meritFrom,
          remark: this.editForm.remark,
        });
        if (res.flag !== true) {
          return this.$message.error("更新绩效信息失败");
        }
        //关闭对话框
        this.editDialogVisible = false;
        //提示更新绩效信息成功
        this.$message.success("更新绩效信息成功");
        //获取新的更新绩效信息
        this.getUserList();
      });
    },
    //删除绩效信息
    async removeId(id) {
      console.log(id);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该绩效信息, 是否继续?",
        "删除绩效信息",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post("admin/deleteMerit/", {
        id: id,
      });
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除绩效信息信息失败");
      }
      this.$message.success("删除绩效信息信息成功");
      this.getUserList();
    },
  },
};
</script>

<style></style>
